<template>
    <view>
        <!--<nui-step :step="3" :class="{ pt40: isSign }"></nui-step>-->
        <nui-step :step="stepType" :class="{ pt40: isSign }"></nui-step>
        <u-top-tips ref="uTips" navbar-height="0"></u-top-tips>
        <text class="loading-box" v-if="isShowLoading">
            <u-loading mode="circle" size="150" color="#2b85e4"></u-loading>
            <view class="text">协议生成中，请稍后···</view>
        </text>

        <view class="pdf-box" :class="{ h80: !isSign }">
            <show-pdf class="iframe" ref="pdf" v-for="i in numPages" :key="i" :src="url" :page="i"></show-pdf>
        </view>

        <text v-if="!isSign" class="fixed">
            <text class="tips">提示：手指上下滑动查看更多内容。</text>
            <u-button type="primary" shape="square" :ripple="true" size="medium" class="custom-btn" @click="goSign">前往签名
            </u-button>
        </text>

        <text v-else class="fixed">
            <u-button type="primary" shape="square" :ripple="true" size="medium" class="custom-btn" @click="toPath">下一步
            </u-button>
        </text>
    </view>
</template>

<script>
import ShowPdf from "vue-pdf";
import { mapGetters } from "vuex"
import AuthInfo from "@/utils/authInfo.js";
import { getQueryString } from "@/utils/index.js";
import {getUrlParam} from "../../utils";

export default {
    data() {
        return {
            isShowLoading: true,
            url: "", //https://pdf-lib.js.org/assets/with_update_sections.pdf http://xsd.jluomi.com/xsdcrm-wechat-ui/1.pdf
            numPages: null, // pdf 总页数,
            deptId: null
        };
    },
    components: {
        ShowPdf,
    },
    computed: {
        ...mapGetters(["isSign", "stepType"]),
    },
    async mounted() {

        // this.$store.commit("UPDATE_SIGN_STATUS", true);
        // document.body.addEventListener(
        //   "touchmove",
        //   (e) => {
        //     e.preventDefault();
        //   },
        //   { passive: false }
        // );
        this.getDeptId()
        // 查询税务认证信息
        await this.syncTaxCert()
        // 津昌人力先刷脸再签署
        if ('7368' === this.deptId || '7432' === this.deptId || '7433' === this.deptId) {
            if (AuthInfo.getEnableTax() === true) {
                const res = await this.getNeedCert();
                const needCert = res && res.code === 200 && res.data.need === true
                // 跳转到税务认证
                if (needCert) {
                    await this.getTaxDept()
                    return
                }
            }
        }
        let refresh = uni.getStorageSync("refresh");
        if (refresh == "1") {
            this.getInduction()

        }
        await this.getPdf();
        // this.url = "https://pdf-lib.js.org/assets/with_update_sections.pdf";
        // this.getNumPages("https://pdf-lib.js.org/assets/with_update_sections.pdf");
    },
    methods: {
        async getInduction() {
          try {
            // this.form = {
            //   idCardName: '',
            //   idCardNo: '',
            // }
            // const {data} = await this.$api.riders.getInduction()
            // this.form = {...data}
            // if(this.form.status == '40'){
            //   let query = {
            //     inductionId : this.form.inductionId
            //   }
              // const res = await this.$api.riders.webaccessInduction(query)

              let selectDeptId = uni.getStorageSync("selectDeptId")
              uni.setStorageSync("refresh", "");
              console.log("type==>",getUrlParam("type"))
              let type = getUrlParam("type")
              if (type) {
                window.location.href = `https://crm.xinlanrenli.com/xsdcrm-wechat-ui/?deptId=${selectDeptId?selectDeptId:this.deptId}&type=${type}`
              } else {
                window.location.href = `https://crm.xinlanrenli.com/xsdcrm-wechat-ui/?deptId=${selectDeptId?selectDeptId:this.deptId}`
              }
              return false;

            // }
          } catch (e) {
          }
        },
        getDeptId() {
            this.deptId = getQueryString("deptId")
        },
        async getPdf() {
            let { data } = await this.$api.riders.getLoadPdf();
            if (data.pdfPath == "") {
                let { code, msg } = await this.$api.riders.regenerateData();
                if (code != 200) {
                    uni.showToast({
                        title: msg,
                        icon: "error",
                    });
                    return false;
                }
                this.getPdf()
                return
            } else if (data.isSign == "Y") {
                this.$store.commit("UPDATE_SIGN_STATUS", true);
            }
            this.url = data.pdfPath;
            this.getNumPages(data.pdfPath);
            this.showTopTips();
        },
        showTopTips() {
            if (this.isSign) {
                this.$refs.uTips.show({
                    title: "签约成功",
                    type: "success",
                    duration: "230000",
                });
            }
        },
        goSign() {
            this.$u.route("pages/sign/index");
        },
        async toPath() {
            if (AuthInfo.getEnableTax() === true) {
                const res = await this.getNeedCert();
                const needCert = res && res.code === 200 && res.data.need === true
                // 跳转到税务认证
                if (needCert) {
                    await this.getTaxDept()
                    return
                }
            }
            let downloadShow = AuthInfo.getDownloadShow();
            if (downloadShow == '0') {
                debugger
                await this.$api.riders.otherStatus({ otherStatus: 80 });
                this.$u.route("pages/download/index");
            } else {
                await this.$api.riders.otherStatus({ otherStatus: 90 });
                this.$u.route("pages/finish/index");
            }
        },
        //计算pdf页码总数
        getNumPages(url) {
            let loadingTask = ShowPdf.createLoadingTask(url);
            loadingTask.promise
                .then((pdf) => {
                    this.numPages = pdf.numPages;
                    setTimeout(() => {
                        this.isShowLoading = false;
                    }, 1500);
                })
                .catch((err) => {
                    uni.showToast({
                        title: "协议加载失败",
                        icon: "error",
                    });
                });
        },
        // 查询是否需要税务认证
        async getNeedCert() {
            return await this.$api.tax.getNeedCert()
        },
        // 跳转税务认证
        async getTaxDept() {
            try {
                const res = await this.$api.tax.getTaxDept()
                if (res && res.data) {
                    // 跳转到税务认证
                    let h5Res = await this.$api.tax.getH5Url();
                    if (h5Res && h5Res.code === 200) {
                        // window.open(h5Res.msg, '_self')
                        window.location.href = h5Res.msg
                    }
                }
            } catch (e) {
                uni.showToast({
                    title: "跳转税务认证错误" + e,
                    icon: "error",
                });
            }
        },
        // 同步税务认证信息
        async syncTaxCert() {
            try {
                if (AuthInfo.getEnableTax() === true) {
                    await this.$api.tax.syncCert()
                }
            } catch (e) {
                uni.showToast({
                    title: '同步税务认证错误' + e,
                    icon: "error",
                });
            }
        }
    },
};
</script>

<style lang="scss" scoped>
uni-page-body {
    height: 100%;
}

.u-tips {
    height: auto;
}

.pdf-box {
    position: absolute !important;
    top: 120px;
    height: 68vh;
    overflow-y: scroll;
}

.tips {
    position: absolute;
    bottom: 50px;
    margin: 15px auto;
    left: 50%;
    transform: translateX(-50%);
    width: 80%;
    font-size: 12px;
    color: #666;
    width: 100%;
    text-align: center;
}

.custom-btn {
    position: absolute;
    bottom: 10px;
    margin: 15px auto;
    left: 50%;
    transform: translateX(-50%);
}

.loading-box {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    text-align: center;

    .text {
        margin-top: 10px;
    }
}

.fixed {
    position: fixed;
    width: 100%;
    height: 90px;
    bottom: 0;
    background: #fff;
    border-top: 1px solid #e1e1e1;
}

.pt40 {
    padding-top: 50px;
}

.h80 {
    height: 73vh !important;
}
</style>
